import React from 'react'
import axios from 'axios'
import { withRouter } from 'react-router-dom'
import { loadData } from '../../redux/user.redux'
import { connect } from 'react-redux'

@withRouter
@connect(
  null,
  { loadData }
)
class AuthRoute extends React.Component {
  componentDidMount () {
    let publicList = ['/login', '/register']
    let pathname = this.props.location.pathname
    if (publicList.indexOf(pathname) > -1) {
      return null
    }
    // 获取用户信息
    axios.get('/api/user/info').then(res => {
      if (res.data.code === 0) {
        // 有登录信息
        this.props.loadData(res.data.data)
      } else {
        this.props.history.push('/login')
      }
    })
    // 检测是否登录
    // 现在的 url 地址，如果没登录进 /login
    // 用户的 type，genius or boss
    // 用户是否完善信息
  }
  render () {
    return null
  }
}

export default AuthRoute
